<?php
if(!defined('ADMIN')){
	exit('Access Denied');
}

if($_POST['sub']!=''){
    $id=empty($_POST['id'])?0:(int)$_POST['id'];
	if(isset($_POST['sort']) && $_POST['sort']==''){$_POST['sort']=0;}
	unset($_POST['id']);
	unset($_POST['sub']);
	unset($_POST['act_id']);
	unset($_POST['mod_id']);
	unset($_POST['node_id']);
	unset($_POST['pid']);
	
    
    if(trim($_POST['title'])==""){
        jumpajax('300','菜单名称不能为空','');
    }
    if(trim($_POST['node'])==""){
        jumpajax('300','节点不能为空','');
    }    
	if($id==0){
		if($_POST['act']=='' && $_POST['mod']==''){
		    $_POST['listorder']=$_POST['sort']+10000;
		    unset($_POST['sort']);
	        $menuid=$ezhand->select('menu','id','`node`="'.$_POST['node'].'" and `mod`="" and `act`=""');
		    if($menuid>0){
		        jumpajax(300,'节点已存在');
		    }
		    $data=array('parent_id'=>0,'node'=>$_POST['node'],'mod'=>$_POST['mod'],'act'=>$_POST['act'],'title'=>$_POST['title'],'hide'=>$_POST['hide'],'listorder'=>$_POST['listorder'],'addtion'=>$_POST['addtion'],'topnav'=>$_POST["topnav"]);
			$menuid=$ezhand->insert('menu',$data);
			$data=array('role_id'=>1,'menu_id'=>$menuid);
			$ezhand->insert('menu_access',$data);
	    }
		elseif($_POST['act']=='' || $_POST['mod']==''){
		    jumpajax(300,'缺少模块或行为文件');
		}
	    else{
	        $topnav=$ezhand->select('menu','topnav','node="'.$_POST['node'].'" and parent_id=0');
		    //if($menuid>0){
		   //     jump(-1,'菜单已存在');
		  //  }
			$parent_menuid=$ezhand->select('menu','id','`node`="'.$_POST['node'].'" and `mod`="" and act=""');
		    $data=array('parent_id'=>$parent_menuid,'node'=>$_POST['node'],'mod'=>$_POST['mod'],'act'=>$_POST['act'],'title'=>$_POST['title'],'hide'=>$_POST['hide'],'sort'=>$_POST['sort'],'addtion'=>$_POST['addtion'],'topnav'=>$topnav);
			//print_r($data);exit;
			$menuid=$ezhand->insert('menu',$data);
			$data=array('role_id'=>1,'menu_id'=>$menuid);
			$ezhand->insert('menu_access',$data);
		}
		jumpajax('200','保存成功',"","closeCurrent");
	}
	else{
		if($_POST['act']=='' && $_POST['mod']==''){
		    $_POST['listorder']=$_POST['sort'];
		    unset($_POST['sort']);
            
			$menuid=$ezhand->select('menu','id','`node`="'.$_POST['node'].'" and `mod`="" and `act`="" and id<>"'.$id.'"');
		    if($menuid>0){
		        jumpajax(300,'节点已存在');
		    }
            $ndata=array("topnav"=>$_POST["topnav"]);
            $ezhand->update(MOD,$ndata,'`node`="'.$_POST['node'].'"');
            
		}
		elseif($_POST['act']=='' || $_POST['mod']==''){
		    jumpajax(300,'缺少模块或行为文件');
		}
	    else{
	       $_POST["topnav"]=$ezhand->select('menu','topnav','node="'.trim($_POST['node']).'" and parent_id=0');
           
		}
		$_POST['parent_id']=$ezhand->select('menu','id','`node`="'.$_POST['node'].'" and `mod`="" and act=""');
		if($_POST['parent_id']==$id){$_POST['parent_id']=0;}
	    $ezhand->update(MOD,$_POST,'id="'.$id.'"');
		jumpajax('200','修改成功','','closeCurrent');
	}
}
else{
	$id=empty($_GET['id'])?0:(int)$_GET['id'];
	$mod_arr=$ezhand->select_2_field('menu','id,`mod`','`mod`<>"" group by `mod`');
	$node_arr=$ezhand->select_2_field('menu','node,`title`','`parent_id`=0 order by `listorder` desc');
    //echo 123123123;
    if($id==0){
	    $row=array();
	}
	else{
	    $row=$ezhand->select(MOD,'*','id="'.$id.'"');
	}
}
?>